﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="NameGenerator.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT
{
    #region Using Statements
    using System;

    #endregion

    /// <summary>
    /// Utility class to generate a sequentially numbered series of names
    /// </summary>
    public class NameGenerator
    {
        private static object lockObject = new object();
        private string prefix;
        private int next;

        public NameGenerator(string prefix)
        {
            lock (lockObject)
            {
                this.prefix = prefix;
                this.next = 1;
            }
        }

        public int Next
        {
            get 
            { 
                return this.next; 
            }

            set
            {
                if (this.next != value)
                {
                    lock (lockObject)
                    {
                        this.next = value;
                    }
                }
            }
        }

        public string Prefix
        {
            get 
            { 
                return this.prefix; 
            }

            set
            {
                if (this.prefix != value)
                {
                    lock (lockObject)
                    {
                        this.prefix = value;
                    }
                }
            }
        }

        public string Generate()
        {
            int nextId = System.Threading.Interlocked.Increment(ref this.next);
            return String.Format("{0}{1}", this.prefix, nextId);
        }

        public void Reset()
        {
            lock (lockObject)
            {
                this.next = 1;
            }
        }
    }
}
